{ Crypto and Hashing funtions needed for Bitcoin.

  This unit is mostly a binding to the needed openssl functions,
  and some wrappers and overloaded variations for convenience.
  It links against libssl. The comments next to the imported
  functions are the unmodified comments from the openssl headers,
  some function arguments have been slightly "pascalified", in some
  places the passing of void- or byte-pointers to buffers was
  more appropriately notated as const, var or out parameters.

  ***

  Copyright (C) 2011 Bernd Kreuss <prof7bit@googlemail.com>

  This library is free software; you can redistribute it and/or modify it
  under the terms of the GNU Library General Public License as published by
  the Free Software Foundation; either version 2 of the License, or (at your
  option) any later version with the following modification:

  As a special exception, the copyright holders of this library give you
  permission to link this library with independent modules to produce an
  executable, regardless of the license terms of these independent modules,and
  to copy and distribute the resulting executable under terms of your choice,
  provided that you also meet, for each linked independent module, the terms
  and conditions of the license of that module. An independent module is a
  module which is not derived from or based on this library. If you modify
  this library, you may extend this exception to your version of the library,
  but you are not obligated to do so. If you do not wish to do so, delete this
  exception statement from your version.

  This program is distributed in the hope that it will be useful, but WITHOUT
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
  for more details.

  You should have received a copy of the GNU Library General Public License
  along with this library; if not, write to the Free Software Foundation,
  Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
}
unit bitcoin_crypto;
{$mode objfpc}{$H+}

interface
const
  {$IFDEF UNIX}
    LIBSSL = 'libssl';
  {$ELSE}
    {$IFDEF WIN32}
      LIBSSL = 'libeay32';
    {$ELSE}
      {$ERROR Please send me a patch to make this compile on other platforms}
    {$ENDIF}
  {$ENDIF}

  { Error codes for the ECDSA functions.  }
  { Function codes.  }
  ECDSA_F_ECDSA_DATA_NEW_METHOD = 100;
  ECDSA_F_ECDSA_DO_SIGN = 101;
  ECDSA_F_ECDSA_DO_VERIFY = 102;
  ECDSA_F_ECDSA_SIGN_SETUP = 103;
  { Reason codes.  }
  ECDSA_R_BAD_SIGNATURE = 100;
  ECDSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE = 101;
  ECDSA_R_ERR_EC_LIB = 102;
  ECDSA_R_MISSING_PARAMETERS = 103;
  ECDSA_R_NEED_NEW_SETUP_VALUES = 106;
  ECDSA_R_RANDOM_NUMBER_GENERATION_FAILED = 104;
  ECDSA_R_SIGNATURE_MALLOC_FAILED = 105;

  { Curve name (there exist more but we only use this one in bitcoin) }
  NID_secp256k1 =   714;

type
  TSHA256Digest = array[1..32] of Char;
  TRIPEMD160Digest = array[1..20] of Char;

  PPBIGNUM = ^PBIGNUM;
  PBIGNUM = Pointer;

  PPECDSA_SIG = ^PECDSA_SIG;
  PECDSA_SIG = ^TECDSA_SIG;
  TECDSA_SIG = record
    r: PBigNum;
    s: PBigNum;
  end;

  PEC_KEY = Pointer;

{ Hash functions }

function SHA256(const Msg; MsgSize:SizeInt; out Digest):PByte; cdecl; external LIBSSL;
function RIPEMD160(const Msg; MsgSize:SizeInt; out Digest):PByte; cdecl; external LIBSSL;

{ EC and ECDSA related functions }

function EC_KEY_new_by_curve_name(NID: Integer): PEC_KEY; cdecl; external LIBSSL;

(* EC_KEY_generate_key() creates a ec private (public) key *)
function EC_KEY_generate_key(key: PEC_KEY): Integer; cdecl; external LIBSSL;

(* ECDSA_SIG *ECDSA_SIG_new(void)
 * allocates and initialize a ECDSA_SIG structure
 * \return pointer to a ECDSA_SIG structure or NULL if an error occurred
 *)
function ECDSA_SIG_new: PECDSA_SIG; cdecl; external LIBSSL;

(* ECDSA_SIG_free
 * frees a ECDSA_SIG structure
 * \param a pointer to the ECDSA_SIG structure
 *)
procedure ECDSA_SIG_free(a:PECDSA_SIG); cdecl; external LIBSSL;

(* i2d_ECDSA_SIG
 * DER encode content of ECDSA_SIG object (note: this function modifies *pp
 * ( *pp += length of the DER encoded signature)).
 * \param a  pointer to the ECDSA_SIG object
 * \param pp pointer to a unsigned char pointer for the output or NULL
 * \return the length of the DER encoded ECDSA_SIG object or 0
 *)
function i2d_ECDSA_SIG(a:PECDSA_SIG; pp:PPbyte):longint; cdecl; external LIBSSL;

(* d2i_ECDSA_SIG
 * decodes a DER encoded ECDSA signature (note: this function changes *pp
 * ( *pp += len)).
 * \param v pointer to ECDSA_SIG pointer (may be NULL)
 * \param pp buffer with the DER encoded signature
 * \param len bufferlength
 * \return pointer to the decoded ECDSA_SIG structure (or NULL)
 *)
function d2i_ECDSA_SIG(v:PPECDSA_SIG; pp:PPbyte; len:longint):PECDSA_SIG; cdecl; external LIBSSL;

(* ECDSA_do_sign
 * computes the ECDSA signature of the given hash value using
 * the supplied private key and returns the created signature.
 * \param dgst pointer to the hash value
 * \param dgst_len length of the hash value
 * \param eckey pointer to the EC_KEY object containing a private EC key
 * \return pointer to a ECDSA_SIG structure or NULL
 *)
function ECDSA_do_sign(const dgst; dgst_len:SizeInt; eckey:PEC_KEY):PECDSA_SIG; cdecl; external LIBSSL;

(* ECDSA_do_verify
 * verifies that the supplied signature is a valid ECDSA
 * signature of the supplied hash value using the supplied public key.
 * \param dgst pointer to the hash value
 * \param dgst_len length of the hash value
 * \param sig  pointer to the ECDSA_SIG structure
 * \param eckey pointer to the EC_KEY object containing a public EC key
 * \return 1 if the signature is valid, 0 if the signature is invalid and -1 on error
 *)
function ECDSA_do_verify(const dgst; dgst_len:SizeInt; sig:PECDSA_SIG; eckey:PEC_KEY):longint; cdecl; external LIBSSL;

(* ECDSA_size
 * returns the maximum length of the DER encoded signature
 * \param  eckey pointer to a EC_KEY object
 * \return numbers of bytes required for the DER encoded signature
 *)
function ECDSA_size(eckey:PEC_KEY):longint; cdecl; external LIBSSL;


implementation

end.
